﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using HotelMgr.Models;

namespace HotelMgr.Areas.Admin.Controllers
{
    //[Authorize(Roles = "Administrators")]
    public class CustomerManagerController : Controller
    {
        private HotelEntities db = new HotelEntities();

        //
        // GET: /CustomerManager/
        public ViewResult Index()
        {
            return View(db.Customers.ToList());
            //return View();
        }

        //
        // GET: /CustomerManager/Details/5

        public ViewResult Details(Guid id)
        {
            Customer customer = db.Customers.Find(id);
            return View(customer);
        }

        //
        // GET: /CustomerManager/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /CustomerManager/Create

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                customer.CustomerID = Guid.NewGuid();
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(customer);
        }
        
        //
        // GET: /CustomerManager/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Customer customer = db.Customers.Find(id);
            return View(customer);
        }

        //
        // POST: /CustomerManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(customer);
        }

        //
        // GET: /CustomerManager/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Customer customer = db.Customers.Find(id);
            return View(customer);
        }

        
         //POST: /CustomerManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Customer customer = db.Customers.Find(id);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        #region Grid
        public ActionResult Select()
        {
            return View("Index",db.Customers.ToList());
        }
        public ActionResult Insert()
        {
            Customer customer=new Customer();
            if (TryUpdateModel(customer))
            {
                customer.CustomerID = Guid.NewGuid();
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("Index",db.Customers.ToList());
        }
        public ActionResult Save(Guid id)
        {
            Customer customer = new Customer {CustomerID = id};
            if(TryUpdateModel(customer))
            {
                    db.Entry(customer).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");  
            }

            return View("Index",db.Customers.ToList());
        }
        public ActionResult Delete()
        {
            
            return View("Index",db.Customers.ToList());
        }
        #endregion



        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}